<template>
    <div style="height:100%">

        <el-alert
                title="题目添加表单"
                type="success">
        </el-alert>
        <br>
        <div class="cateform" style="width:50%">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

                <el-form-item label="标题" prop="question">
                    <el-input v-model="ruleForm.question"></el-input>
                </el-form-item>
                <el-form-item label="关键字" prop="keywords">
                    <el-input v-model="ruleForm.keywords"></el-input>
                </el-form-item>
                <el-form-item label="题目类型" prop="ruleForm.cate_id">
                    <el-checkbox-group v-model="ruleForm.cate_id">
                        <el-checkbox v-for="list in catelists"
                                     :key="list.id"
                                     :label="list.id"
                                     name="answer">{{ list.name }}</el-checkbox>
                    </el-checkbox-group>
                </el-form-item>
                <el-form-item label="章节">
                    <el-select v-model="ruleForm.chapters_id" placeholder="请选择章节">
                        <div v-for="chapter in chapters" :key="chapter.id">
                            <el-option v-bind:label="chapter.name" v-bind:value="chapter.id"></el-option>
                        </div>
                    </el-select>
                </el-form-item>
                <el-form-item label="图片" prop="url">
                    <el-upload
                            class="upload-demo"
                            action="imageupload"
                            :on-preview="handlePreview"
                            :on-remove="handleRemove"
                            :on-success = 'handleSuccess'
                            :file-list="fileList2"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <input type="hidden" v-model="ruleForm.url" prop="url">
                <el-form-item label="选项一" prop="item1">
                    <el-input v-model="ruleForm.item1"></el-input>
                </el-form-item>
                <el-form-item label="选项二" prop="item2">
                    <el-input v-model="ruleForm.item2"></el-input>
                </el-form-item>
                <el-form-item label="选项三" prop="item3">
                    <el-input v-model="ruleForm.item3"></el-input>
                </el-form-item>
                <el-form-item label="选项四" prop="item4">
                    <el-input v-model="ruleForm.item4"></el-input>
                </el-form-item>
                <el-form-item label="正确答案" prop="answer">
                    <el-checkbox-group v-model="ruleForm.answer">
                        <el-checkbox label="1" name="answer"></el-checkbox>
                        <el-checkbox label="2" name="answer"></el-checkbox>
                        <el-checkbox label="3" name="answer"></el-checkbox>
                        <el-checkbox label="4" name="answer"></el-checkbox>
                    </el-checkbox-group>
                </el-form-item>
                <el-form-item label="官方解释" prop="explains">
                    <el-input type="textarea" v-model="ruleForm.explains"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>

        </div>
    </div>
</template>

<script>

    export default {
        mounted(){
            axios.post('/api/GetSubFourCate').then(rew=>{
                this.catelists = rew.data.cates
            })
            axios.post('/api/GetSubFourChapters').then(rew=>{
                this.chapters = rew.data.chapters
            })
        },
        data() {
            return {
                catelists:[],
                chapters:[],
                ruleForm: {
                    question: '',
                    keywords:'',
                    cate_id: [],
                    keywords:'',
                    item1:'',
                    item2:'',
                    item3:'',
                    item4:'',
                    answer: [],
                    url:'',
                    chapters_id:'',
                    explains:'',
                },
                fileList: {
                    name: '',
                    url: ''
                },
                rules: {
                    question: [
                        { required: true, message: '请输入标题', trigger: 'blur' },
                    ],
                    keywords:[
                        { required: true, message: '请输入关键字', trigger: 'change'}
                    ],
                    item1: [
                        { required: true, message: '请输入选项一', trigger: 'change' }
                    ],
                    answer:[
                        { required: true, message: '请勾选正确选项',}
                    ],
                    item2: [
                        { required: true, message: '请输入选项二', trigger: 'change' }
                    ],
                    explains:[
                        {required:true, message: '请输入官方解释',}
                    ],

                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post('/api/SubFourAdd',this.ruleForm).then(rew=>{
                            if(rew.data==200){
                                this.open3();
                            }
                            if(rew.data==400){
                                this.open4();
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            open3() {
                this.$notify({
                    title: '添加成功',
                    message: '点击关闭',
                    type: 'success'
                });
            },
            open4() {
                this.$notify({
                    title: '题目已存在',
                    message: '点击关闭',
                    type: 'error'
                });
            },
            handleRemove(file, fileList) {
                this.ruleForm.url = '';

            },
            handlePreview(file) {
                console.log(file);
            },
            handleSuccess(file){
                this.ruleForm.url = file.id;
            }
        }
    }
</script>

